#coding=utf8
""" Перша гра з Pygame """
 
from __future__ import division
import pygame
import random
import sys
from pygame.locals import *
import math

math.sqr = lambda x: x*x
 
class palette(): #Палітра кольорів
        black=(0,0,0) #Запис кольору відповідно до моделі RGB
	red = (255,0,0)
	blue = (0,0,255)
 
resolution=(300,300) # Роздільна здатність (розмір вікна)
 
pygame.init() # Підвантажуємо все що треба
 
screen=pygame.display.set_mode(resolution,0,32) # Створюємо наше вікно

points = []
running = True
N = 10

def gistogram(points,cl, axis=0, n=N):
	res = [0]*n	
	for p in points:
		if p[1]==cl:
			if axis==0:
				res[int(p[0][0]/resolution[0]*n)]+=1;
			if axis==1:
				res[int(p[0][1]/resolution[1]*n)]+=1;
	return map(lambda x: float(x)/n,res)


def P_x(x0, cl,n=N):
	if cl == 1:
		return x_histo_cl1[int(x0/resolution[0]*n)]
	else:
		return x_histo_cl3[int(x0/resolution[0]*n)]

def P_y(x0, cl,n=N):
	if cl == 1:
		return y_histo_cl1[int(x0/resolution[0]*n)]
	else:
		return y_histo_cl3[int(x0/resolution[0]*n)]

Pcl = [0]*4
def get_Pcl():
	Pcl[1] = len(filter(lambda x: x[1] == 1, points)) / len(points)
	Pcl[3] = len(filter(lambda x: x[1] == 3, points)) / len(points)
	return Pcl

def class_possibility_x(cl, x):
	return (P_x(x,cl) * Pcl[cl])/(P_x(x,1)*Pcl[1] + P_x(x,3)*Pcl[3]+0.001)
def class_possibility_y(cl, y):
	return (P_y(y,cl) * Pcl[cl])/(P_y(y,1)*Pcl[1] + P_y(y,3)*Pcl[3]+0.001)

def class_color(x,y):
	cl1 = class_possibility_x(1,x)*class_possibility_y(1,y)
	cl3 = class_possibility_x(3,x)*class_possibility_y(3,y)
	return (int(cl1*255),0,int(cl3*255))

def dist(a,b):
	#return int(math.sqrt(math.sqr(a[0]-b[0])+math.sqr(a[1]-b[1]))) % 30
	return abs(a[0] - b[0]) + abs(a[1] - b[1])


def voronoi(x,y):
	m_dist = 1000
	t = 0
	dists = [dist((x,y),p[0] for p in poinsts]
	for p in points:
		d = dist((x,y),p[0])
		if d< m_dist:
			m_dist=d
			t = p[1]
	
	if border:
		return palette.white
	if t == 1:
		return (int(1020/(m_dist+4)),0,0)
	else:
		return (0,0,int(1020/(m_dist+4)))


def draw_map(surf, mp):
	pxarr = pygame.PixelArray (surf)
	for i in range(resolution[0]):
		for j in range(resolution[1]):
			pxarr[i][j] = mp(i,j)
		pxarr[i][0]=palette.red
	return pxarr.make_surface()

background = screen.copy()

while running: #Нескінченний цикл роботи програми
	screen.fill(palette.black)
        for event in pygame.event.get(): #Обробляємо події які можуть трапитись
                if event.type == pygame.locals.QUIT: # Якщо трапилась подія на зразок клацання по кнопці закрити
			running == False
			break
		elif event.type == MOUSEBUTTONDOWN:
			pos = pygame.mouse.get_pos()
			points.append((pos,event.button))
		elif event.type == KEYDOWN:
			if event.key == K_ESCAPE:
				running=False
				break
			elif event.key == K_g:
				background = draw_map(screen,voronoi)
	screen.blit(background, (0,0))	
	for p in points:
		if p[1] == 1:
			pygame.draw.circle(screen,palette.red,p[0],3)
		elif p[1] == 3:
			pygame.draw.circle(screen,palette.blue,p[0],3)
	pygame.display.update()
pygame.display.quit()
